package lixiang.neurons;

import java.util.Random;

public class ArrayNeurons {

  public static final byte MAX = (byte) 128;
  public static final float EVEN = 0.5f;

  private Random random = new Random();
  private int count = 100;
  private int groupcount = 100;
  private int stepCount = 1000;
  private int[][] statuses;
  private byte[][][] group;
  private float epsilon = 0.5f;

  // private byte[][] connections;

  public ArrayNeurons() {
    // connections = new byte[count][count];
    statuses = new int[groupcount][count];
    group = new byte[groupcount][count][count];
    for (int i = 0; i < groupcount; i++) {
      for (int j = 0; j < count; j++) {
        statuses[i][j] = random.nextInt();
        random.nextBytes(group[i][j]);
        group[i][j][j] = 1;
      }
    }
  }

  public void update(int index) {
    for (int i = 0; i < count; i++) {
      int status = 0;
      for (int j = 0; j < count; j++) {
        if (statuses[index][j] > EVEN) {
          status += group[index][i][j];
        }
      }
      statuses[index][i] = status / count;
    }
  }

  private int getIntragroupStatus(int index, int i) {
    int status = 0;
    for (int j = 0; j < count; j++) {
      if (statuses[index][j] > EVEN) {
        status += group[index][j][i];
      }
    }
    return status / count;
  }

  private int getIntergroupStatus(int index) {
    int status = 0;
    for (int i = 0; i < groupcount; i++) {
      if (statuses[i][index] > EVEN) {
        status++;
      }
    }
    return status / count;
  }

  public void updateGroup() {
    int[][] statuses2 = new int[groupcount][count];
    for (int i = 0; i < count; i++) {
      float status = getIntergroupStatus(i) * epsilon;
      for (int j = 0; j < groupcount; j++) {
        status += getIntragroupStatus(j, i) * (1 - epsilon);
        statuses2[i][j] = (int) status;
      }
    }
    for (int i = 0; i < groupcount; i++) {
      for (int j = 0; j < count; j++) {
        statuses[i][j] = statuses2[i][j];
      }
    }
  }

  public int even(int index) {
    int sum = 0;
    for (int i = 0; i < groupcount; i++) {
      sum += statuses[i][index];
    }
    System.out.println("sum " + sum);
    return sum / groupcount;
  }

  public int diff() {
    int variant = 0;
    for (int j = 0; j < count; j++) {
      int even = even(j);
      System.out.println("even " + even);
      for (int i = 0; i < groupcount; i++) {
        int diff = statuses[i][j] - even;
        variant += diff * diff;
      }
    }
    return variant / groupcount;
  }

  public void run() {
    for (int i = 0; i < stepCount; i++) {

    }
  }

  private static void show() {
    int count = 10000;
    int[] ints = new int[count];
    Random random = new Random();
    for (int i = 0; i < count; i++) {
      ints[i] = random.nextInt() % 255;
    }
    new DataView(ints).show();
  }

  private void show2() {
    int[] diffs = new int[stepCount];
    for (int i = 0; i < stepCount; i++) {
      updateGroup();
      diffs[i] = diff();
    }
    new DataView(diffs).show();
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    // Neurons neurons = new Neurons(); neurons.run();
    new ArrayNeurons().show2();
  }
}
